package com.android.boncen.commonFrame.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.boncen.commonFrame.R;
import com.android.boncen.commonFrame.bean.GlucoseHistoryBean;
import com.android.boncen.commonFrame.bean.UserBean;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobUser;

/**
 * 功能描述:0血糖-1血压-2血糖蛋白-3BMI历史适配器
 *
 * @date 2017/2/8
 */

public class GluciseAdapter extends BaseAdapter {

    private LayoutInflater inflater;

    public void setData(List<GlucoseHistoryBean> data) {
        this.data.addAll(data);
        this.notifyDataSetChanged();
    }

    public List<GlucoseHistoryBean> data = new ArrayList<>();

    public void clear()
    {
        data.clear();
        this.notifyDataSetChanged();
    }
    public GluciseAdapter(Context context)
    {
        super();
        inflater =LayoutInflater.from(context);

    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public GlucoseHistoryBean getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHond hond=null;
        if (convertView ==null)
        {
            convertView = inflater.inflate(R.layout.glucise_item,parent,false);
            hond = new ViewHond();
            hond.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
            hond.tvType = (TextView) convertView.findViewById(R.id.tvType);

            convertView.setTag(hond);

        }else
        {
            hond = (ViewHond) convertView.getTag();
        }
        //0血糖-1血压-2血糖蛋白-3BMI
        final GlucoseHistoryBean bean = getItem(position);
        hond.tvTime.setText(bean.getUpdatedAt());


        switch (bean.getTypeBlood())
        {
            case 0:
                String type =   setDataView( bean);
                hond.tvType.setText(Html.fromHtml(type +""+getTextColorSelect(bean.getBloodSugar())));
                break;
            case 1:
                hond.tvType.setText(Html.fromHtml("血压:"+getTextColorXY(bean.getBloodSugar(),bean.getOther())));
                break;
            case 2:
                hond.tvType.setText(Html.fromHtml("血红蛋白:"+getTextColorXH(bean.getBloodSugar())));
                break;
            case 3:
                hond.tvType.setText(Html.fromHtml("BMI 值:"+getTextColorBMI(bean.getBloodSugar())));
                break;
        }



        return convertView;
    }

    //血糖ok
    private String getTextColorSelect(float bloodNum) {


        if (bloodNum > 10.0) {

            return "<font color=\"#FF3030\">" + String.format("%1$2.2f",bloodNum)  + "mmol/L</font>";
        } else if (bloodNum < 4.0) {

            return "<font color=\"#EEAD0E\">" + String.format("%1$2.2f",bloodNum)  + "mmol/L</font>";
        } else {

            return "<font color=\"#228B22\">" + String.format("%1$2.2f",bloodNum)  + "mmol/L</font>";
        }

    }

    //BMI 中国标准ok
    private String getTextColorBMI(float bloodNum) {


        if (bloodNum >= 28.0) {

            //肥胖	>= 28.0
            return "<font color=\"#FF3030\">" + String.format("%1$2.2f",bloodNum)  + "mmol/L 身体肥胖</font>";
        } else if (bloodNum <= 18.4) {

            // 偏瘦	<= 18.4
            return "<font color=\"#EEAD0E\">" + String.format("%1$2.2f",bloodNum)  + "mmol/L 身体偏瘦</font>";
        } else if (24.0<=bloodNum && bloodNum <= 27.9 ) {

            //  过重	24.0 ~ 27.9
            return "<font color=\"#EEAD0E\">" + String.format("%1$2.2f",bloodNum)  + "mmol/L 身体过重</font>";
        }else {

            //正常	18.5 ~ 23.9
            return "<font color=\"#228B22\">" + String.format("%1$2.2f",bloodNum)  + "mmol/L 身体正常</font>";
        }

    }

    //血红蛋白ok
    private String getTextColorXH(float bloodNum) {
        UserBean bmobUser = BmobUser.getCurrentUser(UserBean.class);
        if (bmobUser.getSex())
        {
            //    成年男性：120～160g/L
            if (bloodNum > 160.0) {

                //偏高
                return "<font color=\"#FF3030\">" +  String.format("%1$2.2f",bloodNum) + "g/L 偏高</font> ";
            } else if (bloodNum < 120.0) {

                // 偏低
                return "<font color=\"#EEAD0E\">" + String.format("%1$2.2f",bloodNum) + "g/L 偏低</font> ";
            } else {

                //正常
                return "<font color=\"#228B22\">" +  String.format("%1$2.2f",bloodNum) + "g/L 正常</font> ";
            }
        }else
        {
            //    成年女性：110～150g/L
            if (bloodNum > 160.0) {

                //偏高
                return "<font color=\"#FF3030\">" + String.format("%1$2.2f",bloodNum)  + "g/L 偏高</font> ";
            } else if (bloodNum < 110.0) {

                // 偏低
                return "<font color=\"#EEAD0E\">" + String.format("%1$2.2f",bloodNum)  + "g/L 偏低</font> ";
            } else {

                //正常
                return "<font color=\"#228B22\">" + String.format("%1$2.2f",bloodNum)  + "g/L 正常</font> ";
            }
        }

    }


    // 血压ok
    private String getTextColorXY(float bloodNum,float di) {

//        正常的血压范围是收缩压在90－140mmHg（12.0－18.7kpa）之间，舒张压在60－90mmHg（8.0－12.0kpa）
        String cotent = "";
        if (bloodNum > 140.0) {

            //偏高
            cotent= "<font color=\"#FF3030\">" + String.format("%1$2.2f",bloodNum) + "mmHg 收缩压 偏高</font>";
        } else if (bloodNum < 90.0) {

            // 偏低
            cotent= "<font color=\"#EEAD0E\">" + String.format("%1$2.2f",bloodNum)  + "mmHg 收缩压 偏低</font>";
        } else {

            //正常
            cotent = "<font color=\"#228B22\">" + String.format("%1$2.2f",bloodNum)  + "mmHg 收缩压 正常</font>";
        }


        if (di > 90.0) {

            //偏高
            cotent += "&nbsp;<font color=\"#FF3030\">" + String.format("%1$2.2f",di)  + "mmHg 舒张压 偏高</font>";
        } else if (di < 60.0) {

            // 偏低
            cotent += "&nbsp;<font color=\"#EEAD0E\">" + String.format("%1$2.2f",di)  + "mmHg 舒张压 偏低</font>";
        } else {

            //正常
            cotent += "&nbsp;<font color=\"#228B22\">" + String.format("%1$2.2f",di)  + "mmHg 舒张压 正常</font>";
        }

        return cotent;
    }


    private String setDataView(GlucoseHistoryBean bean) {

            switch (bean.getType()) {
                case 0:
                    return "早餐 前";
                case 1:
                    return "早餐 后";
                case 2:
                    return "午餐 前";
                case 3:
                    return "午餐 后";
                case 4:
                    return "晚餐 前";
                case 5:
                    return "晚餐 后";
                case 6:
                    return "睡前 前";
            }
        return "";
    }
    static  class ViewHond
    {
        TextView tvTime;
        TextView tvType;

    }

}
